<template>
  <div class="login-container">
    <div class="nav">
      <el-row type="flex" justify="center">
        <div class="nav-main">
          <div class="nav-brand">
            <a href="/"><img src="./img/logo.png" alt="..." /></a>
          </div>
          <div class="nav-text">
            <a href="/">{{ $t("top_title") }}</a>
          </div>
          <div class="nav-lang">
            <lang-select class="right-menu-item hover-effect" />
          </div>
        </div>
      </el-row>
    </div>

    <div class="main-part">
      <el-row type="flex" justify="center">
        <!-- 图片 -->
        <img
          src="./img/banner01.png"
          class="banner-img"
          alt="..."
          height="400"
          width="600"
        />
        <!-- 登录窗 -->
        <!-- 整个登录表 -->
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <!-- 开头的logo和文字 -->
          <div class="title-container">
            <img
              src="./img/登录页Logo-new.png"
              alt="..."
              height="64"
              width="64"
            />
            <h3 class="title">{{ $t("center_title") }}</h3>
          </div>

          <!-- 账号框 -->
          <el-form-item prop="username">
            <el-row type="flex" justify="center" :gutter="0">
              <el-col :span="18">
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  :placeholder="$t('telephone_or_email')"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                />
              </el-col>
            </el-row>
          </el-form-item>

          <!-- 密码框 -->
          <el-form-item prop="password">
            <el-row type="flex" justify="center" :gutter="0">
              <el-col :span="18">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  :placeholder="$t('password')"
                  name="password"
                  tabindex="2"
                  auto-complete="on"
                />
              </el-col>
            </el-row>

            <!-- 小眼睛 -->
            <el-row type="flex" justify="end" style="display: flex">
              <el-col :span="2" :pull="2">
                <span class="show-pwd" @click="showPwd">
                  <svg-icon
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center" :gutter="0">
              <el-col :span="12">
                <el-input
                  placeholder="请输入验证码"
                  v-model="loginForm['code']"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                  @keyup.enter.native="handleLogin"
                ></el-input>
              </el-col>
              <el-col :span="6">
                <img
                  :src="`data:image/png;base64,${captchaImg}`"
                  alt="验证码"
                  @click="getCaptcha"
                  style="cursor: pointer; width: 100%"
                />
              </el-col>
            </el-row>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-row type="flex" justify="center">
            <el-col :span="18">
              <el-button
                :loading="loading"
                type="primary"
                style="width: 100%; margin-bottom: 25px"
                @click.native.prevent="handleLogin"
                >{{ $t("login") }}
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-row>
    </div>

    <!-- 底部 -->
    <footer>
      <div class="footer-brand">
        Copyright © All Right Reserved by
        <a href="http://www.free4inno.com/" target="_blank">自邮之翼</a>
      </div>
    </footer>
  </div>
</template>

<script>
import md5 from "js-md5";
import LangSelect from "@/components/LangSelect";
import { getCaptchaImg } from "@/api/user";
export default {
  name: "Login",
  components: { LangSelect },
  data() {
    return {
      loginForm: {
        uuid: "",
        code: "",
        username: "",
        password: "",
      },
      captchaImg: "",
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            message: this.$t(
              "please_enter_the_correct_telephone_number_or_email"
            ),
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("please_enter_the_password"),
          },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  mounted() {
    this.getCaptcha();
  },
  methods: {
    getCaptcha() {
      getCaptchaImg().then((response) => {
        console.log("getCaptchaImg------>", response);
        this.captchaImg = response.data.image_base64;
        this.loginForm.uuid = response.data.uuid;
        // this.loginForm.code = response.data.code;
      });
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          const dataForm = {
            username: this.loginForm.username,
            password: md5(this.loginForm.password),
            uuid: this.loginForm.uuid,
            code: this.loginForm.code,
          };
          console.log("dataForm------->", dataForm);

          this.$store
            .dispatch("user/login", dataForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch((error) => {
              this.$message({
                message: error,
                type: "error",
              });
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<!-- <style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style> -->

<style lang="scss" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background-color: #eeeeee;

  // 顶部logo
  .nav {
    height: 50px;
    background-color: white;
    .nav-main {
      width: 1000px;
      .nav-brand {
        padding-top: 9px;
        float: left;
      }
      .nav-text {
        padding-left: 5px;
        padding-top: 14px;
        float: left;
        font-weight: bold;
        font-size: 20px;
      }
      .nav-lang {
        padding-top: 14px;
        float: right;
        font-size: 20px;
      }
    }
  }

  // 主体
  .main-part {
    background-color: #eeeeee;
    // 登录表
    .login-form {
      height: 400px;
      width: 400px;
      background-color: white;
      padding-top: 20px;

      .title-container {
        text-align: center;
      }

      // 输入框样式
      .el-input {
        display: inline-block;
        height: 47px;
        width: 100%;

        input {
          background: transparent;
          border: 0px;
          -webkit-appearance: none;
          border-radius: 0px;
          padding: 12px 5px 12px 15px;
          color: #000;
          height: 47px;
          caret-color: #000;

          &:-webkit-autofill {
            box-shadow: 0 0 0px 1000px #fff inset !important;
            -webkit-text-fill-color: #000 !important;
          }
        }
      }

      // 表单项样式
      .el-form-item {
        position: relative; // 设置相对定位
        border-radius: 5px;

        // 验证错误信息样式
        .el-form-item__error {
          position: absolute !important; // 确保绝对定位
          top: 100% !important; // 使错误信息位于输入框下方
          left: 0 !important; // 使错误信息与输入框左边对齐
          margin-top: 5px !important; // 为错误信息和输入框之间添加一些间距
        }

        // 小眼睛图标样式
        .show-pwd {
          position: absolute;
          top: 50%; // 垂直居中
          transform: translateY(-50%);
          cursor: pointer;
        }
      }
    }
  }

  // 底部
  footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 50px;
    padding: 15px;
    background-color: white;
    z-index: 9999;

    .footer-brand {
      text-align: center;
    }
  }
}

// 媒体查询 xl
@media (min-width: 1920px) {
  .login-container {
    .main-part {
      padding-top: 200px;
    }
  }
}

// lg
@media (min-width: 1100px) and (max-width: 1920px) {
  .login-container {
    .main-part {
      padding-top: 130px;
    }
  }
}

// sm
@media (min-width: 768px) and (max-width: 1100px) {
  .login-container {
    .nav {
      .nav-main {
        width: 884px;
      }
    }
    .main-part {
      padding-top: 90px;
      padding-left: 70px;
      padding-right: 70px;
    }
  }
}

// xs
@media (max-width: 768px) {
  .login-container {
    .nav {
      .nav-main {
        padding-left: 20px;
        padding-right: 20px;
        .nav-text {
          display: none;
        }
      }
    }
    .main-part {
      padding-top: 150px;
      padding-left: 20px;
      padding-right: 20px;
      .banner-img {
        display: none;
      }
    }
  }
}
</style>
